//
//  DetailViewModel.swift
//  CodingTest
//
//  Created by LYPC on 2021/11/19.
//

import UIKit
import JXPhotoBrowser

final class DetailViewModel: BaseViewModel {
    
    var model = DataModel()
    
    func getNumberOfRows() -> Int {
//        var num = model.content.isEmpty ? 0 : 1
//        let imgCount = model.imgUrls.count > 0 ? 1 : 0
//        num += imgCount
        return 2
    }
    
    func getHeightForRow(_ indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return model.contentLHeight_detail > 0 ? model.contentLHeight_detail+20.fitR+10.fitR : 0
        } else if indexPath.row == 1 {
            let imgCount = model.imgUrls.count
            let imgItemH = kScreenWid-2*20.fitR
            return imgCount.fitR*imgItemH
        } else {
            return 0
        }
    }
    
    ///点击图片大图浏览
    func showImg(tableView: UITableView, indexPath: IndexPath, currentIdx: Int) {

        let browser = JXPhotoBrowser()
        browser.numberOfItems = {
            self.model.imgUrls.count
        }
        browser.pageIndex = currentIdx
        browser.pageIndicator = JXPhotoBrowserNumberPageIndicator()
        browser.reloadCellAtIndex = { [weak self] context in
            guard let self = self else { return }
            let browserCell = context.cell as? JXPhotoBrowserImageCell
            let indePath = IndexPath(item: context.index, section: indexPath.section)
            browserCell?.imageView.kf.setImage(with: self.model.imgUrls[indePath.item].asUrl,
                                               placeholder: UIImage(named: "placeholder_empty"))
        }
        browser.show()
    }
}
